$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'vipuser/vipinfos/list',
        datatype: "json",
        colModel: [
			{ label: 'vipID', name: 'vipinfoid', index: 'VipInfoId',key:true, width: 40},
			{ label: '用户ID', name: 'userinfoid', index: 'UserInfoId', width: 80 },
            { label: '用户账户', name: 'useraccount', index: 'UserAccount', width: 80 },
            { label: '用户名', name: 'username', index: 'UserName', width: 80 },
            { label: '性别', name: 'usergender', index: 'UserGender',align:"center", width: 40,formatter: function(value, options, row){
                    return value === 0 ? '男' : '女';
                } },
            { label: '积分余额', name: 'integralnumber', index: 'IntegralNumber', width: 80 },
            { label: '手机号码', name: 'phonenum', index: 'PhoneNum', width: 80 },
			{ label: '会员申请价格', name: 'priceinvip', index: 'PriceInVip', width: 80,formatoptions:{decimalSeparator:".", thousandsSeparator: " ", decimalPlaces: 2, defaulValue: 0},formatter:'integer' },
			{ label: '处理人', name: 'dealusername', index: 'DealUserName', width: 60 },
            { label: '创建时间', name: 'createtime', index: 'CreateTime', width: 90 },
            { label: '过期时间', name: 'expirationtime', index: 'ExpirationTime', width: 90 },
            { label: '软删除', name: 'isdel', index: 'IsDel', width: 50 ,formatter: function(value, options, row){
                    if(value === 0){
                        return  '<span class="label label-success">正常</span>';
                    }else if(value === 1){
                        return   '<span class="label label-danger">已删除</span>';
                    }else{
                        return '<span class="label label-danger">错误</span>';
                    }
                }},
			{ label: '审核状态', name: 'auditstatus', index: 'AuditStatus', width: 80 ,formatter: function(value, options, row){
                var status = null;
                if (value === 0){
                    status = '<span class=".label label-warning">待审核</span>';
				}else if(value === 1){
                    status = '<span class="label label-success">审核通过</span>';
				}else if (value === 2){
                    status = '<span class="label label-danger">审核失败</span>';
				}else{
                    status = '<span class="label label-danger">错误</span>';
                }
				return status;
                }},
			{ label: '审核状态时间', name: 'auditdate', index: 'AuditDate', width: 90 },
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
	    isVipExpiration:-1,
        username:null,
		showList: true,
		title: null,
		vipinfos: {},
        months:0,
		btn:null,
        searchParam:null,
        usersetdatatext:{},
        selectUserSearchParam:null
	},
	methods: {
        showVipExpirationTime:function () {
            var months = parseInt(vm.months);
            var timeIntervals = 31;//一个月时长
            var date = new Date().AddDays(months * timeIntervals).ToString('yyyy-MM-dd hh:mm:ss');
            vm.vipinfos.expirationtime = date;
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.months = 0;
            vm.btn = 'new';
			vm.vipinfos = {userinfoid:null,isdel:0,auditstatus:0,expirationtime:null,useraccount:null};
		},
		update: function (event) {
			var vipinfoid = getSelectedRow();
			if(vipinfoid == null){
				return ;
			}
            vm.months = 0;
            vm.btn = "update";
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(vipinfoid);
		},
		saveOrUpdate: function (event) {
			var url = vm.vipinfos.vipinfoid == null ? "vipuser/vipinfos/save" : "vipuser/vipinfos/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.vipinfos),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var vipinfoids = getSelectedRows();
			if(vipinfoids == null){
				return ;
			}
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "vipuser/vipinfos/delete",
                    contentType: "application/json",
				    data: JSON.stringify(vipinfoids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(vipinfoid){
			$.get(baseURL + "vipuser/vipinfos/info/"+vipinfoid, function(r){
                vm.vipinfos = r.vipinfos;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				url:baseURL + "vipuser/vipinfos/list?isVipExpiration="+vm.isVipExpiration
                + ((vm.searchParam != null)? "&searchParam=" + vm.searchParam:""),
                page:page
            }).trigger("reloadGrid");
		},
        selectUser:function () {
        	//新增才能选择用户
        	if (vm.btn == 'new'){
                //弹窗数据请求的url
                var selectUrl = 'user/userinfos/list';
                //要显示的弹窗数据列
               var tableList = [
                { label: '用户ID', name: 'userinfoid', index: 'UserInfoId', width: 80, key: true },
                { label: '用户账户', name: 'useraccount', index: 'UserAccount', width: 60 },
                { label: '用户名', name: 'username', index: 'UserName', width: 60 },
                { label: '性别', name: 'usergender', index: 'UserGender',align:"center", width: 20,formatter: function(value, options, row){
                        return value === 0 ? '男' : '女';
                } },
                { label: '手机号码', name: 'phonenum', index: 'PhoneNum', width: 80 },
                { label: '是否Vip', name: 'isvip', index: 'IsVip', width: 40 ,formatter: function(value, options, row){
                        return value === 0 ?
                        '<span class="label label-info">普通用户</span>' :
                        '<span class="label label-primary">VIP用户</span>';
                }}
                ];
                window.T.selectLayer(selectUrl,tableList,"selectLayer","selectLayerPager");
                layer.open({
                    type: 1,
                    offset: '50px',
                    skin: 'layui-layer-molv',
                    title: "选择用户",
                    area: ['67%', '85%'],
                    shade: 0,
                    shadeClose: false,
                    content: jQuery("#selectUserID"),
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        var row = window.T.getSelectedRow("selectLayer");
                        if (row != null){
                            vm.vipinfos.userinfoid = row.userinfoid;
                            vm.vipinfos.useraccount = row.useraccount;
                        }
                        layer.close(index);
                    }
                });
			}
        },
        selectUserQuery:function () {
            var page = $("#selectUser").jqGrid('getGridParam','page');
            $("#selectUser").jqGrid('setGridParam',{
                url:baseURL + "user/userinfos/queryUser" + ((vm.selectUserSearchParam != null)? "?searchParam=" + vm.selectUserSearchParam:""),
                page:page
            }).trigger("reloadGrid");
            vm.selectUserSearchParam = null;
        },
        reason:function(){
            if(this.vipinfos == null){
                return false;
            }
            //审核状态当前为不同就显示
            if(this.vipinfos.auditstatus == 2){
                return true;
            }
            //审核状态为待审核，且有原因就显示
            if(this.vipinfos.auditstatus == 0 && this.vipinfos.cannotpassreason != null && this.vipinfos.cannotpassreason != ""){
                return true;
            }
            return false;
        }
	}
});